package cn.aposoft.mq.kafka.admin.consumer;

import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.ConsumerGroupListing;
import org.apache.kafka.clients.admin.ListConsumerGroupOffsetsResult;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.TopicPartition;

import java.util.*;
import java.util.concurrent.ExecutionException;

public class ListConsumerGroupOffsets {

    public static void main(String[] args) {
        Properties p = new Properties();
        p.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "aposoft.cn:10091");// kafka地址，多个地址用逗号分割
        try (Admin admin = Admin.create(p);) {
            List<NewTopic> topics = new ArrayList<>();
            ListConsumerGroupOffsetsResult result = admin.listConsumerGroupOffsets("test");
            KafkaFuture<Map<TopicPartition, OffsetAndMetadata>> allFuture = result.partitionsToOffsetAndMetadata();
            System.out.println("TopicPartition:");
            System.out.println(allFuture.get());
//            KafkaFuture<Collection<ConsumerGroupListing>> validFuture = result.valid();
//            System.out.println(validFuture.get());
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
